DevJourney

C/Functions/Composite numbers upto n/COMPFNC.C

//Program to print composite numbers upto n
#include<stdio.h>
#include<conio.h>
int main() 
{
	int printComp(int),n,p;
	
	printf("Enter a number : ");
	scanf("%d",&n);
	printf("Composite numbers upto %d : ",n);
	p=printComp(n);
	getch();
}
//The printComp function
int printComp(int a) //a is user input
{
	int f,i,temp,d,v;
	//v is is used to keep track of divisors
	for(temp=1;temp<=a;temp++)
		{
			v=0; //Reset divisor count to 0 for the term
			for(d=2;d<temp;d++) //For each term, divisor is 2 to before that term
				{
					if(temp%d==0)	//Divisible
						v++;
				}
			if(v!=0) //Found no divisor
				printf("%d ",temp);
		}
	return(f);
    return 0;
}
View on GitHub